package algorithm.mst.prim;

import java.util.Arrays;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-07 17:31
 * @ Description :
 */
public class PrimAlgo
{
    public static void main(String[] args)
    {
        char[] data = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G'};
        int numOfVertexes = data.length;

        int[][] weight = new int[][]{
                {10000, 5, 7, 10000, 10000, 10000, 2},
                {5, 10000, 10000, 9, 10000, 10000, 3},
                {7, 10000, 10000, 10000, 8, 10000, 10000},
                {10000, 9, 10000, 10000, 10000, 4, 10000},
                {10000, 10000, 8, 10000, 10000, 5, 4},
                {10000, 10000, 10000, 4, 5, 10000, 6},
                {2, 3, 10000, 10000, 4, 6, 10000},
        };
        MGraph graph = new MGraph(numOfVertexes);
        MinTree minTree = new MinTree();
        minTree.createGraph(graph, numOfVertexes, data, weight);
        //minTree.display(graph);

        minTree.prim(graph,0);
    }

}

class MinTree
{
    public void createGraph(MGraph graph, int numOfVertexes, char[] data, int[][] weight)
    {
        for (int i = 0; i < numOfVertexes; i++)
        {
            graph.data[i] = data[i];
            //System.arraycopy(weight[i], 0, graph.weight[i], 0, numOfVertexes);
            for (int j = 0; j < numOfVertexes; j++)
            {
                graph.weight[i][j] = weight[i][j];
            }
        }
    }

    public void display(MGraph graph)
    {
        for (int[] edges : graph.weight)
        {
            System.out.println(Arrays.toString(edges));
        }
    }

    public void prim(MGraph graph, int startVertex)
    {
        int totalWeight = 0;

        int[] isVisited = new int[graph.numOfVertexes];
        isVisited[startVertex] = 1;
        //h1,h2: 记录两个顶点的索引
        int h1 = -1;
        int h2 = -1;

        int minWeight = 10000;

        //n个顶点,n-1个边
        for (int k = 1; k < graph.numOfVertexes; k++)
        {
            //i: 节点i被访问过
            for (int i = 0; i < graph.numOfVertexes; i++)
            {
                //j: 节点j未被访问过
                for (int j = 0; j < graph.numOfVertexes; j++)
                {
                    if (isVisited[i] == 1 &&
                            isVisited[j] == 0 &&
                            graph.weight[i][j] < minWeight)
                    {
                        minWeight = graph.weight[i][j];
                        h1 = i;
                        h2 = j;
                    }
                }
            }
            //每一轮循环都可以找到 所有已访问过的节点中 邻边没有被访问中权值最短的一条边
            System.out.println("边<" + graph.data[h1] + "," + graph.data[h2] + "> 权值为: " + minWeight);
            totalWeight += minWeight;
            isVisited[h2] = 1;
            minWeight = 10000;
        }
        System.out.println("totalWeight = " + totalWeight);
    }
}

class MGraph
{
    public int numOfVertexes;//顶点个数
    public char[] data;//顶点数据域
    public int[][] weight;//邻接矩阵

    public MGraph(int numOfVertexes)
    {
        this.numOfVertexes = numOfVertexes;
        data = new char[numOfVertexes];
        weight = new int[numOfVertexes][numOfVertexes];
    }

}